package com.yiwei.zhh.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.IncomeWorkV3Bean;
import com.yiwei.zhh.api.bean.UploadImageBean;
import com.yiwei.zhh.util.GlideRoundTransform;
import com.yiwei.zhh.util.ImageUtil;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.V19FrameLayout;
import com.yiwei.zhh.view.uploadImage.UploadImgAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3WorkIdentityActivity extends BaseActivity implements OnAddressSelectedListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_work_place)
    EditText editWorkPlace;
    @BindView(R.id.edit_work_time)
    EditText editWorkTime;
    @BindView(R.id.btn_area)
    TextView btnArea;
    @BindView(R.id.edit_address)
    EditText editAddress;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.img_identity)
    ImageView imgIdentity;
    @BindView(R.id.btn_take)
    ImageView btnTake;
    private Unbinder unbinder;
    private BottomDialog bottomDialog;
    private String province;
    private String city;
    private String area;
    String imageUrl;
    private UploadImgAdapter uploadImgAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_identity_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("工作证明");
        bottomDialog = new BottomDialog(this);
        bottomDialog.setOnAddressSelectedListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }


    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }

            @Override
            public void superReject() {

            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    uploadImg(file);
                }
            }
        });
    }

    public void getIncomeWorkV3(String image, String years, String company, String address, String province, String city, String area) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        IncomeWorkV3Bean bean = new IncomeWorkV3Bean();
        bean.image = image;
        bean.years = years;
        bean.company = company;
        bean.address = address;
        bean.province = province;
        bean.city = city;
        bean.area = area;
        Api.api_service.getIncomeWorkV3(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "上传成功");
                    finish();
                });
    }

    public void uploadImg(Bitmap img) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        UploadImageBean bean = new UploadImageBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(img, 100);
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    imageUrl = result.getUrl();
                    Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 5)).into(imgIdentity);
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }

    }

    @OnTextChanged({R.id.edit_work_place, R.id.edit_work_time, R.id.edit_address})
    void afterTextChanged(Editable s) {
        isCheckAll();
    }

    private void isCheckAll() {
        if (!TextUtils.isEmpty(imageUrl)&&!TextUtils.isEmpty(UI.toString(editWorkPlace)) && !TextUtils.isEmpty(UI.toString(editWorkTime)) && !TextUtils.isEmpty(UI.toString(editAddress)) && !TextUtils.isEmpty(UI.toString(btnArea))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        String s =
                (province == null ? "" : province.name) +
                        (city == null ? "" : city.name) +
                        (county == null ? "" : county.name) +
                        (street == null ? "" : street.name);

        if (province != null) {
            this.province = province.name;
        }
        if (city != null) {
            this.city = city.name;
        }
        if (county != null) {
            if (street != null) {
                area = county.name + street.name;
            } else {
                area = county.name;
            }
        }
        btnArea.setText(s);
        isCheckAll();
        bottomDialog.dismiss();
    }

    @OnClick({R.id.btn_take, R.id.btn_back, R.id.btn_area, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_take:
                goToTakePhoto();
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_area:
                bottomDialog.show();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(imageUrl)) {
                    ToastUtil.makeText(this, "请上传工作证明照片");
                    return;
                }

                if (TextUtils.isEmpty(UI.toString(editWorkPlace))) {
                    ToastUtil.makeText(this, "请填写您的单位名称");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editWorkTime))) {
                    ToastUtil.makeText(this, "请填写您在的该单位的工作时长");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(btnArea))) {
                    ToastUtil.makeText(this, "请选择所在地区");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editAddress))) {
                    ToastUtil.makeText(this, "请输入所在位置详细地址");
                    return;
                }
                getIncomeWorkV3(imageUrl, UI.toString(editWorkTime), UI.toString(editWorkPlace), UI.toString(editAddress), province, city, area);
                break;
        }
    }
}
